using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WozLib.UI.Ink.SketchProperties;

namespace WozLib.UI.Controls
{
  /// <summary>
  /// Interaction logic for CornerRoundednessSelector.xaml
  /// </summary>

  public partial class CornerRoundednessSelector : UserControl
  {
    public static readonly DependencyProperty SelectedRoundednessProperty =
            DependencyProperty.Register("SelectedRoundedness", typeof(double), typeof(CornerRoundednessSelector),
      new UIPropertyMetadata(0.1, new PropertyChangedCallback(SelectedRoundedness_Changed)));

    public double SelectedRoundedness
    {
      get { return (double)GetValue(SelectedRoundednessProperty); }
      set { SetValue(SelectedRoundednessProperty, value); }
    }

    public static readonly DependencyProperty CascadingPropertyProperty =
            DependencyProperty.Register("CascadingProperty", typeof(CascadingProperty), typeof(CornerRoundednessSelector));

    public CascadingProperty CascadingProperty
    {
      get { return (CascadingProperty)GetValue(CascadingPropertyProperty); }
      set { SetValue(CascadingPropertyProperty, value); }
    }


    public CornerRoundednessSelector()
    {
      InitializeComponent();
      this.Loaded += new RoutedEventHandler(CornerRoundednessSelector_Loaded);
    }

    void CornerRoundednessSelector_Loaded(object sender, RoutedEventArgs e)
    {
      _ZeroRadio.Checked += new RoutedEventHandler(_ZeroRadio_Checked);
      _OneRadio.Checked += new RoutedEventHandler(_OneRadio_Checked);
      _TwoRadio.Checked += new RoutedEventHandler(_TwoRadio_Checked);
      _FiveRadio.Checked += new RoutedEventHandler(_FiveRadio_Checked);

      _BigSlider.PreviewMouseDown += new MouseButtonEventHandler(_Slider_PreviewMouseDown);

      _BigSlider.PreviewMouseUp += new MouseButtonEventHandler(_Slider_PreviewMouseUp);
    }

    void _Slider_PreviewMouseDown(object sender, MouseButtonEventArgs e)
    {
      if (CascadingProperty != null)
      {
        CascadingProperty.InContinuousChange = true;
      }
    }

    void _Slider_PreviewMouseUp(object sender, MouseButtonEventArgs e)
    {
      if (CascadingProperty != null)
      {
        CascadingProperty.InContinuousChange = false;
      }
    }

    void _FiveRadio_Checked(object sender, RoutedEventArgs e)
    {
      if (!_suspendSet)
      {
        this.SelectedRoundedness = 20.0;
      }
    }

    void _TwoRadio_Checked(object sender, RoutedEventArgs e)
    {
      if (!_suspendSet)
      {
        this.SelectedRoundedness = 10.0;
      }
    }

    void _OneRadio_Checked(object sender, RoutedEventArgs e)
    {
      if (!_suspendSet)
      {
        this.SelectedRoundedness = 5.0;
      }
    }

    void _ZeroRadio_Checked(object sender, RoutedEventArgs e)
    {
      if (!_suspendSet)
      {
        this.SelectedRoundedness = 0.0;
      }
    }

    private bool _suspendSet = false;

    private static void SelectedRoundedness_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {

      if (d is CornerRoundednessSelector && e.NewValue is double)
      {
        CornerRoundednessSelector cornerRoundednessSelector = d as CornerRoundednessSelector;
        double roundedness = (double)e.NewValue;
        if (!cornerRoundednessSelector._suspendSet)
        {
          cornerRoundednessSelector._suspendSet = true;

          // Zero
          if (roundedness < 0.05)
          {
            cornerRoundednessSelector._ZeroRadio.IsChecked = true;
          }
          // One
          else if (roundedness < 6.00 && roundedness > 4.00)
          {
            cornerRoundednessSelector._OneRadio.IsChecked = true;
          }
          // Two
          else if (roundedness < 11.00 && roundedness > 9.00)
          {
            cornerRoundednessSelector._TwoRadio.IsChecked = true;
          }
          // Five
          else if (roundedness < 21.00 && roundedness > 19.00)
          {
            cornerRoundednessSelector._FiveRadio.IsChecked = true;
          }
          else
          {
            cornerRoundednessSelector._OtherRadio.IsChecked = true;
          }

          cornerRoundednessSelector._suspendSet = false;
        }
      }
    }

  }
}